<?php

namespace App\Http\Controllers\Admin;
use App\Model\UserVideoWatchRecord;
use App\Model\Video;
use App\Model\Users;
use App\User;
use App\Model\Company;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Model\ImageNew;
use App\Model\Statistics;
class SelfWatchRecordsController extends Controller
{
    /**
     * 用户自查自己看学习情况
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function reportlist(Request $quest)
    {
        $cid = $quest->input("id");
        $data_user = Company::where('id', $cid)->select(array('id','comName'))->first();
        $comName = '';
        if($data_user){
            $comName = $data_user['comName'];
        }
        return view('admin/selfWatchRecords/reportlist')
            ->with('comName',$comName)
            ->with('cid',$cid);;
    }

    /**
     * @param Request $quest
     * @return mixed
     */
    public function reportlistdata(Request $request)
    {
        $cid = $request->input("id");
        $query=UserVideoWatchRecord::where('companyId',$cid)->orderby('id','desc');
        $res=$query->paginate($request->get('limit',15))->toArray();
        //查询视频
        $video=Video::get()->toArray();
        $videoData = array_column($video,'videoName','id');
        foreach ($res['data'] as $key=>$val){
            $videoName = isset($videoData[$val['video_id']]) ? $videoData[$val['video_id']] : '';
            $res['data'][$key]['video_title'] = $videoName;
        }
        $data = [
            'code' => 0,
            'msg'   => '请求成功',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * 用户学习情况统计表
     * @param Request $request
     * @return void
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public function exportone(Request $request)
    {
        $cid = $request->input("id");
        $data_user = Company::where('id', $cid)->select(array('id','comName'))->first();
        $comName = '';
        if($data_user){
            $comName = $data_user['comName'];
        }

        $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        $objPHPExcel = $objReader->load("word/selfWatchRecords.xlsx");
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle($comName."用户学习情况统计表");
        $objActSheet = $objPHPExcel->getActiveSheet();
        $task = UserVideoWatchRecord::where('companyId',$cid)->get()->toarray();
        //查询视频
        $video=Video::get()->toArray();
        $videoData = array_column($video,'videoName','id');
        foreach ($task as $key=>$val){
            $videoName = isset($videoData[$val['video_id']]) ? $videoData[$val['video_id']] : '';
            $task[$key]['video_title'] = $videoName;
        }
        $row = 2;
        $i = 1;
        foreach($task as $tt)
        {
            $objPHPExcel->getActiveSheet()->setCellValue("A".$row,$i);
            $objPHPExcel->getActiveSheet()->setCellValue("B".$row,$tt['video_title']);
            $objPHPExcel->getActiveSheet()->setCellValue("C".$row,$tt['watch_seconds']);
            $objPHPExcel->getActiveSheet()->setCellValue("D".$row,$tt['frequency']);
            $row++;
            $i++;
        }
        for ($x=1; $x<=($row); $x++) {
            $objActSheet->getRowDimension($x)->setRowHeight(30);//统一设置行高度
        }
        $styleArray = array(
            'borders' => array(
                'allborders' => array(
                    'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
                ),
            ),
        );
        $objActSheet->getStyle('A1:E'.($row+10))->applyFromArray($styleArray);//设置粗边框
        $objActSheet->getRowDimension(1)->setRowHeight(30);//单独设置行高度
        $write = new \PHPExcel_Writer_Excel5($objPHPExcel);
        ob_end_clean();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename='.$comName.'"用户学习情况统计表.xls"');//可以让浏览器弹出下载窗口
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');

        exit();
    }

    /**
     * 天数与分钟数
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function statisticslist(Request $quest)
    {
        return view('admin/selfWatchRecords/statisticslist');
    }

    /**
     * @param Request $quest
     * @return mixed
     */
    public function statisticslistdata(Request $request)
    {
        $query=Statistics::orderby('id','desc');
        $res=$query->paginate($request->get('limit',15))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '请求成功',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * 修改页面
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function statisticsedit(Request $request)
    {
        $type = $request->input("type",1);
        $de=Statistics::find($request['id']);
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'self_time' => 'required',
                'study_duration' => 'required'
            ],[
                'self_time.required' => '自查时间，天数必填',
                'study_duration.required' => '学习时长，分钟数必填'
            ]);

            $de->self_time=$request['self_time'];
            $de->study_duration=$request['study_duration'];
            $de->save();
            return redirect()->to(route('admin.selfWatchRecords.statisticslist'))->with(['status'=>'修改成功']);
        }
        return view('admin/selfWatchRecords/statisticsedit')
            ->with('de',$de)
            ->with('type',$type);
    }

}
